package system

import (
	"MoSkeleton/models/base"
	"gorm.io/gorm"
)

type SysRoleMenu struct {
	RoleId uint64 `gorm:"column:role_id;type:BIGINT;NOT NULL"`
	MenuId uint64 `gorm:"column:menu_id;type:BIGINT;NOT NULL"`
}

func (b *SysRoleMenu) TableName() string {
	return "sys_role_menu"
}

/*
select count(1) from sys_role_menu where menu_id = #{menuId}
*/
func (b *SysRoleMenu) CheckMenuExistRole(menuId uint64) (bool, error) {
	var count int64
	err := base.BaseDb.Model(b).Where("menu_id = ?", menuId).Count(&count).Error
	return count > 0, err
}

func (b *SysRoleMenu) AddBatch(tx *gorm.DB, roleMenus []*SysRoleMenu) (int64, error) {
	if len(roleMenus) == 0 {
		return 0, nil
	}
	result := base.GetModelDB(tx).Create(roleMenus)
	return result.RowsAffected, result.Error
}

func (b *SysRoleMenu) DeleteByRoleIds(tx *gorm.DB, roleIds *[]uint64) (bool, error) {
	return base.GetModelDB(tx).Where("role_id in (?)", *roleIds).Delete(b).RowsAffected > 0, nil
}
